package com.example.jacky.uilibrary.utils;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadManager {
	public static ThreadManager instance = new ThreadManager();
	public static ThreadManager getInstance() {
		return instance;
	}
	
	private ThreadPoolProxy longPool;
	private ThreadPoolProxy shortPool;
	//联网
	public synchronized ThreadPoolProxy createLongPool() {
		if(longPool == null) {
			longPool = new ThreadPoolProxy(5, 5, 5000L);
		}
		return longPool;
	}
	//操作本地
	public synchronized ThreadPoolProxy createShortPool() {
		if(shortPool == null) {
			shortPool = new ThreadPoolProxy(3, 3, 5000L);
		}
		return shortPool;
	}
	
	
	public class ThreadPoolProxy {
		private ThreadPoolExecutor pool;
		private int corePoolSize;
		private int maximumPoolSize;
		private long time;

		public ThreadPoolProxy(int corePoolSize, int maximumPoolSize, long time) {
			this.corePoolSize = corePoolSize;
			this.maximumPoolSize = maximumPoolSize;
			this.time = time;
		}
		public void executor(Runnable runnable){
			if(pool == null) {
				// 创建线程池
				/*
				 * 1. 线程池里面管理多少个线程2. 如果排队满了, 额外的开的线程数3. 如果线程池没有要执行的任务 存活多久4.
				 * 时间的单位 5 如果 线程池里管理的线程都已经用了,剩下的任务 临时存到LinkedBlockingQueue对象中 排队
				 */
				pool = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, time, TimeUnit.MILLISECONDS,
						new LinkedBlockingQueue<Runnable>(10));
			}
			pool.execute(runnable);
		}
		public void cancel(Runnable runnable) {
			if(pool != null && !pool.isShutdown() && !pool.isTerminated()) {
				pool.remove(runnable);
			}
		}
	}
}
